package com.websocket;

import com.alibaba.fastjson.JSONObject;
import com.enums.WebSocketMessageEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author Fang Ruichuan
 * @date 2022-09-24 10:52
 */
@Component
@Slf4j
@EnableScheduling
public class WebSocketTask {

    /**
     * 每1秒进行一次websocket心跳检测
     * @author Fang Ruichuan
     * @date 2022/9/24 11:31
     */
    @Scheduled(cron = "0/4 * * * * ?")
    public void clearOrders() {
        int num = 0;
        try {
            JSONObject jsonObject = WebSocketMessageEnum.HEART_CHECK.getJsonValue();
            num = WebSocketServer.sendPing(jsonObject.toJSONString());
        } finally {
            System.out.println("websocket心跳检测结果，共【" + num + "】个连接");
        }
    }
}
